package cn.hs.servlet;

import cn.hs.dao.ShopDao;
import cn.hs.dao.impl.ShopDaoImpl;
import cn.hs.entity.Shop;
import cn.hs.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class shopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        ShopDao shopDao = new ShopDaoImpl();
        if ("querySearchShopList".equals(action)){
            Map<String,String> map = new HashMap<>();
            String keyword = req.getParameter("keyword");
            String stratTime = req.getParameter("stratTime");
            String endTime = req.getParameter("endTime");
            String phone = req.getParameter("phone");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("keyword",keyword);
            map.put("stratTime",stratTime);
            map.put("endTime",endTime);
            map.put("phone",phone);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            //连接数据库

            List<Shop> list = shopDao.selectShop(map);
            int total = shopDao.selectShopCount(map);
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
            //把结果集合，封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("addCourierStation".equals(action)) {
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            Shop shop=new Shop(
                    shopName,shopDescribe,shopImage,shopPhone,
                    shopProvince,shopCity,shopDistrict,shopAddress,shopLongitude,
                     shopLatitude
            );
            int row = shopDao.insertShop(shop);
            if(row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else{
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }

        }
        else if ("modifyShop".equals(action)) {
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopId = req.getParameter("shopId");
            Shop shop=new Shop(
                    Integer.parseInt(shopId),shopName,shopDescribe,shopImage,shopPhone,
                    shopProvince,shopCity,shopDistrict,shopAddress,shopLongitude,
                    shopLatitude
            );
            int row = shopDao.updateShop(shop);
            if(row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else{
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
